<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入真实名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="familyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="真实名字" align="center" prop="name"/>

      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img :src="scope.row.avatar" style="width:300px"/>
            <img :src="scope.row.avatar" style="max-width: 50px;max-height: 50px" slot="reference">
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === '1' ? '男' : '女' }}</span>
        </template>
      </el-table-column>>

      <el-table-column label="出生日期" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile"/>
      <el-table-column label="基础疾病" align="center" prop="basicDisease"/>
      <el-table-column label="医疗机构" align="center" prop="institutionName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['qiushi:user:memberfamily:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户家人对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入真实名字"/>
        </el-form-item>

        <el-form-item label="头像" prop="avatar">
          <image-upload limit="1" v-model="form.avatar"/>
        </el-form-item>

        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码"/>
        </el-form-item>
        <el-form-item label="基础疾病" prop="basicDisease">
          <el-input v-model="form.basicDisease" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
<!--        <el-form-item label="医疗机构" prop="institutionName">-->
<!--          <el-input v-model="form.institutionName" placeholder="请输入医疗机构名称"/>-->
<!--        </el-form-item>-->

        <el-form-item label="医疗机构" prop="deviceType">
          <el-select v-model="form.institutionId" placeholder="请选择类型">
            <el-option
              v-for="item in institution_dict"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFamily, getFamily, delFamily, addFamily, updateFamily} from "@/api/qiushi/user/memberfamily";
import {listInstitution} from "@/api/qiushi/system/qiushiinstitution";

export default {
  name: "Memberfamily",
  data() {
    return {
      // useless
      institution_dict: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户家人表格数据
      familyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        avatar: null,
        sex: null,
        birthday: null,
        mobile: null,
        basicDisease: null,
        institutionId: null,
        institutionName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sex: [
          {required: true, message: "用户性别 1男 2女不能为空", trigger: "change"}
        ],
        institutionId: [
          {required: true, message: "医疗机构id不能为空", trigger: "blur"}
        ],
        institutionName: [
          {required: true, message: "医疗机构名称不能为空", trigger: "blur"}
        ],
        delFlag: [
          {required: true, message: "0正常 1删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户家人列表 */
    getList() {
      this.loading = true;

      listInstitution().then(response => {
        this.institution_dict = response.rows;
      });

      listFamily(this.queryParams).then(response => {
        this.familyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        avatar: null,
        sex: null,
        birthday: null,
        mobile: null,
        basicDisease: null,
        institutionId: null,
        institutionName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,

        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户家人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFamily(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户家人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          /** 图片上传 */
          // if (this.form.avatar != null && this.form.avatar.startsWith("/profile/upload"))
          //   this.form.avatar = process.env.VUE_APP_BASE_API + this.form.avatar

          for (let i = 0; i < this.institution_dict.length; i++) {
            if (this.institution_dict[i].id === this.form.institutionId) {
              this.form.institutionName = this.institution_dict[i].name
              break
            }
          }

          if (this.form.id != null) {
            updateFamily(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFamily(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户家人编号为"' + ids + '"的数据项？').then(function () {
        return delFamily(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qiushi/user/memberfamily/export', {
        ...this.queryParams
      }, `family_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
